package com.smarthome.api.mobile.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;

import com.smarthome.common.mobile.Service;
import com.smarthome.core.dictionary.ServiceTypeService;
import com.smarthome.core.propertymanager.PropertyServiceService;
import com.smarthome.domain.bean.ShService;
import com.smarthome.domain.bean.ShServiceType;
import com.smarthome.domain.enums.ServiceStatusEnum;
import com.smarthome.domain.mobile.MobileCodeEnum;
import com.smarthome.domain.mobile.MobileRequest;
import com.smarthome.domain.mobile.MobileResponse;
import com.smarthome.domain.mobile.req.MobilePropertyServiceRequest;

/**
 * 
* @ClassName: MobilePropertyServiceService
* @Description: 物业服务移动端service
* @author cyj
* @date 2015年10月13日 下午6:45:15
*
 */
@org.springframework.stereotype.Service("mobile.propertyService.service")
public class MobilePropertyServiceService implements Service<MobileResponse<?>> {

	@Resource
	private PropertyServiceService propertyServiceService;
	
	@Resource
	private ServiceTypeService serviceTypeService;
	
	
	/**
	 * 
	* @Title: getList 
	* @Description: 获取物业服务列表
	* @param request
	* @param response
	* void
	* @throws 
	* @author cyj
	 */
	public void getList(MobilePropertyServiceRequest request,MobileResponse<List<ShService>> response) {
		if(request.getAccountId() == null) {
			response.setMobileCodeObj(MobileCodeEnum.PROPERTY_SERVICE_NOT_ACCOUNT);
			return;
		}
		ShService queryService = new ShService();
		BeanUtils.copyProperties(request, queryService);
		queryService.setResidenceId(request.getAccount().getResidenceId());
		queryService.setOrderField("gmtModify");
		queryService.setOrderFieldType("desc");
		int count = propertyServiceService.count(queryService);
		if(request.getStartIndex() != null && request.getStartIndex() >= count) {
			//response.setMobileCodeObj(MobileCodeEnum.PAGE_NOTSHOW);
			response.setData(new ArrayList<ShService>());
			return;
		}
		try {
			List<ShService> propertyServiceList = propertyServiceService.getPropertyServiceList(queryService);
			response.setData(propertyServiceList);
		}catch(Exception e) {
			e.printStackTrace();
			response.setMobileCodeObj(MobileCodeEnum.SYSTEM_ERROR);
		}
	}
	
	
	/**
	 * 
	* add
	* @Title: add
	* @Description: 添加物业服务
	* @param request
	* @param response
	* void
	* @throws
	* @author cyj
	* @date 2015年10月14日 下午7:27:02
	 */
	public void add(MobilePropertyServiceRequest request,MobileResponse<?> response) {
		if(request.getAccountId() == null) {
			response.setMobileCodeObj(MobileCodeEnum.PROPERTY_SERVICE_NOT_ACCOUNT);
			return;
		}
		if(request.getServiceTypeId() == null) {
			response.setMobileCodeObj(MobileCodeEnum.PROPERTY_SERVICE_NOT_TYPE);
			return;
		}
		if(StringUtils.isBlank(request.getContent())) {
			response.setMobileCodeObj(MobileCodeEnum.PROPERTY_SERVICE_NOT_CONTENT);
			return;
		}
		ShService newService = new ShService();
		BeanUtils.copyProperties(request, newService);
		newService.setStatus(ServiceStatusEnum.APPLICATION.getCode());
		Date date = new Date();
		newService.setGmtCreate(date);
		newService.setGmtModify(date);
		newService.setResidenceId(request.getAccount().getResidenceId());
		
		int result = 0;
		try {
			result = propertyServiceService.insert(newService);
		}catch(Exception e) {
			response.setMobileCodeObj(MobileCodeEnum.SYSTEM_ERROR);
			return;
		}
		
		if(result != 1) {
			response.setMobileCodeObj(MobileCodeEnum.PROPERTY_SERVICE_ADD_FAIL);
			return;
		}
		
	}
	
	/**
	 * 
	* getTypeList
	* @Title: getTypeList
	* @Description: 获取物业服务类型列表
	* @param request
	* @param response
	* void
	* @throws
	* @author cyj
	* @date 2015年10月14日 下午8:58:44
	 */
	public void getTypeList(MobileRequest request,MobileResponse<List<ShServiceType>> response) {
		ShServiceType queryServiceType = new ShServiceType();
		queryServiceType.setIsDel(0L);
		try {
			List<ShServiceType> serviceTypes = serviceTypeService.queryServiceType(queryServiceType);
			response.setData(serviceTypes);
		}catch(Exception e) {
			response.setMobileCodeObj(MobileCodeEnum.SYSTEM_ERROR);
		}
		
	}
}
